import java.util.*;

/**
 * @program: leetcode621
 * @author: JCM
 * @create: 2023-02-22 18:14
 * @description:
 **/
public class Main {
    public int leastInterval(char[] tasks, int n) {
        if(n < 1 || tasks.length <= 1) {
            return tasks.length;
        }
        int[] counts = new int[26];
        for(char ch : tasks) {
            counts[ch-'A']++;
        }
        Arrays.sort(counts);
        int maxCount = counts[25];
        int i = 24;
        int retCount = (maxCount-1)*(n+1)+1;
        while(i >= 0 && counts[i] == maxCount) {
            retCount++;
            i--;
        }
        return Math.max(retCount,tasks.length);

    }
}
